import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class A_3 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        List<Module> list = new ArrayList<>();
        int l,r;
        int t = 100000;
        for (int i = 0; i < n; i++) {
            l = sc.nextInt();
            r = sc.nextInt();
            if ((l+r)>0)
                list.add(new Module(l,r));
        }
        int[][] dp = new int[list.size()+1][200001];
        for (int i = 0; i < list.size()+1; i++) {
            for (int j = 1; j < 100001; j++) {
                dp[i][j] = -99999999;
            }
        }
        for(int i=0;i<list.size();i++){
            dp[i][t+list.get(i).ai] = list.get(i).bi;
        }
        for (int i = 1; i < list.size()+1 ; i++) {
            for (int j = 1; j < dp[0].length; j++) {
                dp[i][j] = Math.max(dp[i][j],dp[i-1][j]);
                if(j-list.get(i-1).ai<0||j-list.get(i-1).ai>200000)
                    continue;
                    dp[i][j]=Math.max(dp[i][j],dp[i-1][j-list.get(i-1).ai]+list.get(i-1).bi);
            }
        }
        int re = -999999;
        for(int i=0;i<200001;i++){
            if(dp[list.size()][i]>0){
                re = Math.max(re,dp[list.size()][i]+i);
                System.out.println(dp[list.size()][i]);
                System.out.println("---");
                System.out.println(i);
            }

        }
        System.out.println(re);
    }

    static class Module{
        public Module(int ai, int bi) {
            this.ai = ai;
            this.bi = bi;
        }

        int ai;
        int bi;
    }
}
